﻿using Storage.Core.Data;
using Storage.Mapping;
using Storage.Mapping.Category;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; 

namespace Storage.Services.Category
{
    public class CategoryService : ICategoryService
    {  
        private IRepository<Entities.Category> _categoryRepository;
        private IRepository<Entities.SysPermission> _permissionRepository;

        public CategoryService(IRepository<Entities.Category> categoryRepository,
            IRepository<Entities.SysPermission> permissionRepository)
        {
            this._categoryRepository = categoryRepository;
            this._permissionRepository = permissionRepository;
        }

        ///// <summary>
        ///// 
        ///// </summary>
        ///// <returns></returns>
        //public List<CategoryMapping> getAllCache()
        //{
        //    return _cacheManager.get<List<CategoryMapping>>(MODEL_KEY, () =>
        //    {
        //        if (_categoryRepository.Table.Any())
        //        {
        //            return _categoryRepository.Table.Select(item => new CategoryMapping()
        //            {
        //                Id = item.Id,
        //                ApplicationId = item.ApplicationId,
        //                Name = item.Name,
        //                RouteName = item.RouteName,
        //                Action = item.Action,
        //                Controller = item.Controller,
        //                CssClass = item.CssClass,
        //                FatherResource = item.FatherResource,
        //                IsMenu = item.IsMenu,
        //                Sort = item.Sort,
        //                SysResource = item.SysResource,
        //                AppKey = item.Application.ApKey,
        //                ApplicationName = item.Application.Name
        //            }).ToList();
        //        }
        //        return null;
        //    });
        //}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="appkey"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public void initCategory(List<CategoryMapping> list)
        { 
            var oldList = _categoryRepository.Table.ToList();
            oldList.ForEach(del =>
            {
                var item = list.FirstOrDefault(o => o.SysResource == del.SysResource);
                if (item == null)
                {
                   var permissionList = del.SysPermissions.ToList();
                    permissionList.ForEach(delPrm =>
                    {
                        _permissionRepository.Entities.Remove(delPrm);
                    });
                    _categoryRepository.Entities.Remove(del);
                }
            });
            list.ForEach(entity =>
            {
                var item = oldList.FirstOrDefault(o => o.SysResource == entity.SysResource);
                if (item == null)
                {  
                    _categoryRepository.Entities.Add(entity.toEntity());
                }
                else
                {
                    item.Action = entity.Action;
                    item.Controller = entity.Controller;
                    item.CssClass = entity.CssClass;
                    item.FatherResource = entity.FatherResource;
                    item.IsMenu = entity.IsMenu;
                    item.Name = entity.Name;
                    item.RouteName = entity.RouteName;
                    item.SysResource = entity.SysResource;
                    item.Sort = entity.Sort;
                    item.FatherID = entity.FatherID;
                    item.ResouceID = entity.ResouceID;
                }
            });
            if (_categoryRepository.DbContext.ChangeTracker.HasChanges())
                _categoryRepository.DbContext.SaveChanges();
        }
    }
}
